//
//  JCLHomeViewController.swift
//  WeiBo
//
//  Created by DDC on 2017/9/6.
//  Copyright © 2017年 DDC. All rights reserved.
//

import UIKit
import HMPhotoBrowser

/// 原创微博id
private let cellId = "cellId"
/// 转发微博id
private let retweetedCellId = "retweetedCellId"

class JCLHomeViewController: JCLBaseViewController {
    
    //懒加载
    lazy var listViewModel = JCLStatusListViewModel()

    
    override func viewDidLoad() {
        super.viewDidLoad()
      
        // MARK:  - 注册通知
        NotificationCenter.default.addObserver(self, selector: #selector(imageViewClickNotification), name: NSNotification.Name(rawValue: JCLStatusCellBrowserPhotoNotification), object: nil)
    }
    
    // 移除通知
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    // 通知实现方法
    @objc fileprivate func imageViewClickNotification(n:Notification) {
        
        guard let selectedIndex = n.userInfo?[JCLStatusSelectedIndexNotificationKey] as? Int ,
            let urls = n.userInfo?[JCLStatusURLsNotificationKey] as? [String],
            let imageViewList = n.userInfo?[JCLStatusImageViewsNotificationKey] as? [UIImageView]
        else {
            return
        }
        // 第三方框架图片点击展示
        let vc = HMPhotoBrowserController.photoBrowser(withSelectedIndex: selectedIndex, urls: urls, parentImageViews: imageViewList)
        // 弹出图片显示
        present(vc, animated: true, completion: nil)
        
    }
    func showFriends()  {
        
        let vc = JCLDemoViewController()
        
        navigationController?.pushViewController(vc, animated: true)
    }
    //重写父类方法
    override func loadData() {
        
        
        listViewModel.loadStatus(pullUp: isPullUp) { (_ isSuccess, _ shouldRefresh) in
            
            self.refreshControl?.endRefreshing()
            //恢复状态
            self.isPullUp = false
            
            if shouldRefresh {
                
                self.tableView?.reloadData()
            }
        }
     
    }
    
}

// MARK:  - tableView数据原方法
extension JCLHomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        // 取出视图模型
        let viewModel = listViewModel.statusList[indexPath.row]
        // 根据模型来判断是否转发微博
        let cellIdentifier = viewModel.status.retweeted_status != nil ? retweetedCellId : cellId
        
        let cell =  tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! JCLStatusCell
        
        
        cell.viewModel = viewModel
        
        
        return cell
        
    }
    
    override  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let vm = listViewModel.statusList[indexPath.row]
        
        return vm.rowHeight
    }
}

extension JCLHomeViewController {
    
    override func setUpTableView() {
        
        super.setUpTableView()
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", fontSize: 16, taret: self, action: #selector(showFriends))
        
        tableView?.showsVerticalScrollIndicator = false
        
        /// 原创微博
        let nib = UINib(nibName: "JCLStatusNormalCell", bundle: nil)
        
        tableView?.register(nib, forCellReuseIdentifier: cellId)
//        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        
        /// 转发微博
        let nib2 = UINib(nibName: "JCLStatusRetweetedCel", bundle: nil)
        
        tableView?.register(nib2, forCellReuseIdentifier: retweetedCellId)
        //预估行高
//        tableView?.rowHeight = UITableViewAutomaticDimension
        tableView?.estimatedRowHeight = 300
        //去掉分割线
        tableView?.separatorStyle = .none
        setUpNavTitle()
        
    }
    
    fileprivate func setUpNavTitle () {
        
        let title = JCLNetWorkTools.shared.userAccount.screen_name
        
        let button = JCLNavTitleButton(title: title)
        
        navItem.titleView = button
        
        button.addTarget(self, action: #selector(titleButtonClick), for: .touchUpInside)
    }
  
    func titleButtonClick(btn:UIButton) {
        
        btn.isSelected = !btn.isSelected
    }
    
}
